home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Aminet 21
/
Aminet 21 (1997)(GTI - Schatztruhe)[!][Oct 1997].iso
/
Aminet
/
util
/
cli
/
cc.lha
/
CC.C
< prev
next >
Wrap
C/C++ Source or Header
|
1996-12-03
|
3KB
|
150 lines
/*CC v.1.0 - Numeric standart converter by Adam Kubiczek
(c) 1996 by Adam Kubiczek
Usage: CC fxxxx [std]
where f is number of system and xxxx is number, avaiable systems:
$ - hex (hexdecimal)
& - oct
% - bin (binary)
# - dec (decimal) (also without # number is decimal i.g. CC 512 is the same CC #512)
or Usage: CC !aaxxxx [std]
where ! is char '!', xxxx is number and aa is system of number, i.g.:
CC !161ff is the same CC $1ff
CC !10512 is the same CC #512 or CC 512
CC !021111 is the same CC %1111
etc.
[std] is a system to which the number will converted. It's not requeired, if it not
exists program will print result in DEC,HEX,OCT and BIN system.
(sorry 4 my english!)
*/
#define NO_PC
#include <string.h>
#include <stdlib.h>
#ifdef NO_PC
#include <proto/dos.h>
#else
#include <STDIO.h>
#endif
char Wynik[32];
unsigned char VER[]="$VER: CalcConv V1.0 (01.12.96) by Adam Kubiczek";
unsigned long pot(int a, short j)
{
short i;
unsigned long w=1;
for (i=1;i<=j;i++) w=w*a;
return (w);
}
unsigned long Conv2Dec(char *liczba)
{
short i,j=0,k=1;
int system;
unsigned long wynik=0,m=0;
switch (liczba[0]) {
case '#': system=10;break;
case '$': system=16;break;
case '&': system=8;break;
case '%': system=2;break;
case '!': {system=Conv2Dec( (strncpy (Wynik,liczba,3))+1);k=3;break;}
default : {system=10;k=0;break;}
}
for (i=strlen(liczba)-1;i>=k;i--) {
m=liczba[i];
m=(m<58 ? m-48 : m-87);
wynik=wynik+(m*pot(system,j));
j++;
}
return(wynik);
}
void Conv2Other(unsigned long liczba, short system)
{
int i=0;
unsigned long r,a;
char b;
strnset(Wynik,0,31);
do {
a=liczba/system;
r=liczba-(a*system);
liczba=a;
Wynik[i]=(r<10 ? r+48 : r+87);
i++;
} while (liczba!=0);
for (a=0;a<(i/2);a++) {
b=Wynik[a];
Wynik[a]=Wynik[i-1-a];
Wynik[i-1-a]=b;
}
}
main(int argc, char *argv[])
{
unsigned long decliczba;
unsigned char usersystem=0;
#ifdef NO_PC
if (argc<2) {PutStr("Usage: CC cXXXX [std], where c is #,$,% or &.\n");return(5);}
#else
if (argc<2) {printf("Usage: CC cXXXX [std]\n");return(5);}
#endif
if (argc=3) usersystem=atoi(argv[2]);
strlwr(argv[1]);
decliczba=Conv2Dec(argv[1]);
#ifdef NO_PC
Conv2Other(decliczba,10);
PutStr("Dec:");
PutStr(Wynik);
if (usersystem>1) {
Conv2Other(decliczba,usersystem);
PutStr("\nUser:");PutStr(Wynik);PutStr("\n");
} else {
Conv2Other(decliczba,16);
PutStr("\nHex:");
PutStr(Wynik);
Conv2Other(decliczba,8);
PutStr("\nOct:");
PutStr(Wynik);
Conv2Other(decliczba,2);
PutStr("\nBin:");
PutStr(Wynik);
PutStr("\n");
}
#else
printf("Dec:%d",decliczba); //poniewaû IBM nie ma systemu
printf("\nHex:%x",decliczba); //to uûywamy standartowych funkcji C
printf("\nOct:%o",decliczba); //dziëki czemu wydîuûamy program 6x
Conv2Other(decliczba,2);
printf("\nBin:%s",Wynik);
if (usersystem>1) {
Conv2Other(decliczba,usersystem);
printf("\nUser:%s\n",Wynik);
}
#endif
}